const WebSocket = require('ws')

;((Ws)=>{	
	const server = new Ws.Server({port:8000})
	
	const init=()=>{
		bindEvents()
	}
	
	function bindEvents(){
		server.on('open',handleOpen)
		server.on('close', handleClose)
		server.on('error', handleError)
		server.on('connection', handleConnection)
		
		function handleOpen(){
			console.log('BE: WebSocket open')			
		}
		function handleClose(){
			console.log('BE: WebSocket Close')
		}
		function handleError(){
			console.log('BE: WebSocket Error')
		}
		function handleConnection(ws){
			console.log('BE: WebSocket Connection')			
			ws.send('前端，你好！')			
			ws.on('message', function (msg){
			      console.log('server msg: ', msg)
				  server.clients.forEach(c=>{
				  	c.send(msg)
				  })
		        //  ws.send(msg)
		    })
		}
		
		
	}
	
	init()
})(WebSocket)